
screenCanvas = 0
gameScreen = 0
mapDis = 0
mouseFollower = 0
app = 0

class ClientApp
  constructor: () ->
    @conn = new ClientConnection()
    @InitializeDrawing()
    @openMap("shalala")
  InitializeDrawing: () ->
    @drawTime = 0
    @drawTimeLog = new Date().getTime()
    @nframes = 0
    This = this
    setInterval(`function(){This.redraw()}`,35)

  redraw: () ->
    t0 = new Date().getTime()
    mapDis.draw()
    t1 = new Date().getTime()
    @drawTime+=t1-t0
    @nframes++
    if t1-@drawTimeLog>1000
      $("#DrawTime").html(Math.round(@drawTime/@nframes)+"ms ("+@nframes+")")
      @drawTimeLog = t1
      @drawTime = 0
      @nframes = 0

  openMap: (map) ->
    if map!=null
      $.post("../data/maps.php",{
           action:"open",
           username:"manux",
           password:"tintin",
           map:map},
        (data) ->
          status = data.substring(0,8)
          data = data.substring(8)
          map = JSON.parse(data)
          mapDis.load(map)
          @currentMap = map
      )

exports.init = () ->
  screenCanvas = document.getElementById(screen_name)
  gameScreen = screenCanvas.getContext('2d')
  mapDis = new MapDisplayer(gameScreen)
  mouseFollower = new MouseFollower(screenCanvas)
  exports.app = new ClientApp()
